<template>

    <view class="container">

        <!-- canvas -->
        <canvas class="mycanvas" canvas-id="mycanvas" @touchstart="touchstart" @touchmove="touchmove"
            @touchend="touchend"></canvas>

        <!-- 底部操作按钮 -->
        <view class="footer">
            <view @click='createCanvas()'>开始</view>
            <view class="left" @click="finish">保存</view>
            <view class="right" @click="clear">清除</view>
            <view class="close" @click="close">关闭</view>
        </view>
    </view>

</template>

<script>
    export default {
        name: "my-sign",
        data() {
            return {
                //绘图图像
                ctx: '',
                //路径点集合
                points: [],
                //签名图片
                SignatureImg: ''
            };
        },

        beforeCreate() {
            // 组件实例被创建时执行的代码
            console.log('beforeCreate--------------------------------------')
        },
        mounted() {
            console.log('mounted--------------------------------------')
            this.createCanvas()
        },
        onReady() {
            console.log('onReady--------------------------------------')
        },
        onShow() {
            console.log('onShow--------------------------------------')
        },
        
        methods: {
            createCanvas() {
                console.log('--------------------------------------')
                //创建绘图对象
                this.ctx = uni.createCanvasContext('mycanvas', this);
                //设置画笔样式
                this.ctx.lineWidth = 4;
                this.ctx.lineCap = 'round';
                this.ctx.lineJoin = 'round';
            },
            touchstart(e) {
                let startX = e.changedTouches[0].x;
                let startY = e.changedTouches[0].y;
                let startPoint = {
                    X: startX,
                    Y: startY
                };
                this.points.push(startPoint);
                //每次触摸开始，开启新的路径
                this.ctx.beginPath();
            },
            touchmove(e) {
                let moveX = e.changedTouches[0].x;
                let moveY = e.changedTouches[0].y;
                let movePoint = {
                    X: moveX,
                    Y: moveY
                };
                this.points.push(movePoint); //存点
                let len = this.points.length;
                if (len >= 2) {
                    this.draw(); //绘制路径
                }
            },
            touchend() {
                this.points = [];
            },
            draw() {
                let point1 = this.points[0];
                let point2 = this.points[1];
                this.points.shift();
                this.ctx.moveTo(point1.X, point1.Y);
                this.ctx.lineTo(point2.X, point2.Y);
                this.ctx.stroke();
                this.ctx.draw(true);
            },
            clear() {
                let that = this;
                uni.getSystemInfo({
                    success: function(res) {
                        let canvasw = res.windowWidth;
                        let canvash = res.windowHeight;
                        that.ctx.clearRect(0, 0, canvasw, canvash);
                        that.ctx.draw(true);
                    }
                });
            },
            finish() {
                let that = this;
                uni.canvasToTempFilePath({
                        canvasId: 'mycanvas',
                        success: function(res) {
                            //这里的res.tempFilePath就是生成的签字图片
                            console.log(res.tempFilePath);
                        },
                        fail(e) {
                            console.log(e);
                        }
                    },
                    that);
            }
        }
    };
</script>

<style lang="scss" scoped>
    .container {
        display: flex;
        flex-direction: column;

    }

    .sign-box {
        border: 2rpx solid green;
        width: 300rpx;
        height: 300rpx;
        margin: auto;
        display: flex;
        flex-direction: column;
        text-align: center;
    }

    .sign-view {
        height: 100%;
    }

    .sigh-btns {
        height: 100%;
        margin: auto;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
    }

    .btn {
        margin: auto;
        padding: 8rpx;
        transform: rotate(90deg);
        border: grey 1rpx solid;
    }

    .mycanvas {
        margin: auto 0rpx;
        background-color: #ececec;
    }

    .canvsborder {
        border: 1rpx solid #333;
        position: fixed;
        top: 0;
        // left: 10000rpx;
    }
</style>